<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class ConfigService extends BaseService
{
    //配置

    /**
     * 列表
     * @return array
     */
    public function getList($id)
    {
        $db = $this->dbConn($this->dbConfig)
            ->where('del', 0);
        if ($id) {
            $db = $db->where('id', $id);
        }
        $res = $db->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $val = $v['id']==1 ? toNumber($v['val']) : $v['val'];
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'key' => $v['key'],
                'val' => $val,
            ];
        }
        return $this->toback(0, '', $arys);
    }

    /**
     * 修改
     * @return array
     */
    public function modify($id, $val)
    {
        $res = (array) $this->dbConn($this->dbConfig)->where('id', $id)->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $val = $id==1 ? $val * 100 : $val;
        $this->dbConn($this->dbConfig)->where('id', $id)->update(['val' => $val]);
        return $this->toback();
    }
}